// A minor modification by Kishore Nallan (kishore.nc [(at)] gmail . com)
// See Line 47

/**
 * Vibrate 1.0
 *
 * Makes an element vibrate
 *
 * Usage: jQuery('#my-annoying-ad').vibrate();
 *
 * @class vibrate
 * @param {Object} conf, custom config-object
 *
 * Copyright (c) 2008 Andreas Lagerkvist (andreaslagerkvist.com)
 * Released under a GNU General Public License v3 (http://creativecommons.org/licenses/by/3.0/)
 */
jQuery.fn.vibrate = function(conf) {
        var config = jQuery.extend({
                speed: 30, 
                duration: 2000, 
                frequency: 10000, 
                spread: 3
        }, conf);
        var vTop = '', vLeft = '';

        return this.each(function() {
                var t = jQuery(this);

                var vibrate = function() {
                if (vTop == ''){
                	var offset = t.offset();
                	vTop = offset.top;
                	vLeft = offset.left;
                	
                }
                        //var topPos = Math.floor(Math.random() * config.spread) - ((config.spread - 1) / 2);
                        //var leftPos = Math.floor(Math.random() * config.spread) - ((config.spread - 1) / 2);
                        //t.css({position: 'fixed', left: (50 + leftPos) +'%', top: '50%'});
                        
                        //var topPos = Math.floor(Math.random() * config.spread) - ((config.spread - 1) / 2);
                        var leftPos = Math.floor(Math.random() * config.spread) - ((config.spread - 1) / 2);
                        var rotate = Math.floor(Math.random() * config.spread - (config.spread - 1) / 2); // cheers to erik@birdy.nu for the rotation-idea
                        t.css({position: 'fixed', left: (50 + leftPos) +'%', top: '50%', WebkitTransform: 'rotate(' +rotate +'deg)', '-moz-transform': 'rotate(' + rotate + 'deg)', '-ms-transform': 'rotate('+rotate+'deg)', 'transform': 'rotate('+rotate+'deg)'});
                        
                };

                var doVibration = function () {
                        
						var vibrationInterval = setInterval(vibrate, config.speed);

                        var stopVibration = function() {
                                clearInterval(vibrationInterval);
                                t.css({position: 'fixed', top: '50%', left: '50%', WebkitTransform: 'rotate(0deg)', '-moz-transform': 'rotate(0deg)', '-ms-transform': 'rotate(0deg)', 'transform': 'rotate(0deg)'});
                        };

                        setTimeout(stopVibration, config.duration);
                };
				
				/* 
					Mofication by Kishore - I am commenting out the following line as it calls the vibration function repeatedly.
				 	We need to call it only once. So, instead I make a call to doVibration() directly.
				*/
				
                //setInterval(doVibration, config.frequency);
				doVibration();
        });
};